﻿using FS.Common.DotNetBean;
using FS.Common.DotNetCode;
using FS.Common.DotNetUI;
using FS.Business;
using System;
using System.Collections;
using System.Data;

namespace FS.Web.BLL.Visa_Base
{
    public partial class Base_TrustForm : PageBase
    {
        private string _key;
        visa_dao _visa = new visa_dao();
        protected void Page_Load(object sender, EventArgs e)
        {
            this._key = base.Request["key"];
            if (!base.IsPostBack)
            {
                initBase_trusttype();
                initBase_pricetype();
                initBase_billingOption();
                if (!string.IsNullOrEmpty(this._key))
                {
                    this.InitData();
                }
            }
        }

        //客户类型
        private void initBase_trusttype()
        {
            DataTable dt = _visa.GetSelect_trusttype();
            ControlBindHelper.BindHtmlSelect(dt, this.Ref_trusttype,"property_value", "property_id", "");            
        }
        //价格分类
        private void initBase_pricetype()
        {
            visa_dao visa = new visa_dao();
            DataTable dt = visa.GetSelect_pricetype();
            ControlBindHelper.BindHtmlSelect(dt, this.Ref_pricetype, "property_value", "property_id", "");
        }
                
        //结算方式
        private void initBase_billingOption()
        {
            visa_dao visa = new visa_dao();
            DataTable dt = visa.GetSelect_billingOption();
            ControlBindHelper.BindHtmlSelect(dt, this.Ref_billingOption, "property_value", "property_id", "");
        }

        private void InitData()
        {
            Hashtable ht = DataFactory.Run().GetHashtableById("Base_Trust", "trust_id", this._key);
            if (ht.Count > 0 && ht != null)
            {
                ControlBindHelper.SetWebControls(this.Page, ht);
            }
        }
        protected void Save_Click(object sender, EventArgs e)
        {
            Hashtable ht = new Hashtable();
            ht = ControlBindHelper.GetWebControls(this.Page);

            if (!string.IsNullOrEmpty(this._key))
            {
                ht["trust_id"] = this._key;
                ht["ModifyDate"] = DateTime.Now;
                ht["ModifyUserId"] = RequestSession.GetSessionUser().UserId;
                ht["ModifyUserName"] = RequestSession.GetSessionUser().UserName;
            }
            else
            {
                ht["trust_id"] = CommonHelper.GetGuid;
                ht["CreateUserId"] = RequestSession.GetSessionUser().UserId;
                ht["CreateUserName"] = RequestSession.GetSessionUser().UserName;
                ht["CreateDate"] = DateTime.Now;
            }

            string ls = _visa.Check_Trust(ht["trust_id"].ToString(), ht["trust_code"].ToString());
            if (!string.IsNullOrEmpty(ls))
            {
                ShowMsgHelper.Alert_Error(ls);
            }
            else
            {
                bool IsOk = DataFactory.Run().Submit_AddOrEdit("Base_Trust", "trust_id", this._key, ht);
                if (IsOk)
                {
                    //移除缓存
                    CacheHelper.RemoveAllCache("KeyTrustInfo");
                    ShowMsgHelper.AlertMsg("操作成功！");
                }
                else
                {
                    ShowMsgHelper.Alert_Error("操作失败！");
                }
            }
        }
    }
}